package vn.edu.fpt.xml.back.reader.news;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import vn.edu.fpt.xml.back.reader.Reader;
import vn.edu.fpt.xml.back.reader.exceptions.ReadException;
import vn.edu.fpt.xml.back.dtos.news.Rss;

/**
 * Created by hiephn on 2014/04/24.
 */
public class RssReader implements Reader<Rss> {

    @Override
    public Rss readResource(String resourceUrl) throws ReadException {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Rss> rssResponseEntity = restTemplate.getForEntity(resourceUrl, Rss.class);

        if (rssResponseEntity.getStatusCode() == HttpStatus.OK) {
            return rssResponseEntity.getBody();
        } else {
            throw new ReadException(String.format("Cannot read from %s. HTTP response status: %d. HTTP response body: %s", resourceUrl, rssResponseEntity.getStatusCode().value(), rssResponseEntity.toString()));
        }
    }
}
